package com.example.gpt;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

@RestController
public class QianWenController {

    private final String API_KEY = "sk-4dfd6cf6b93d42cb8639e6acd2f575b0";
    private final String ENDPOINT = "https://tongyi-qiwen-api.aliyun.com/api";

    @GetMapping("/api/search")
    public String callQianWen(@RequestParam("query") String query) {
        // 构建请求参数，可能需要对query加密或签名，根据实际API文档操作
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("query", query);
        params.add("apiKey", API_KEY);

        // 使用RestTemplate或其他HTTP客户端发起请求
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(ENDPOINT, params, String.class);

        // 处理响应结果
        if (response.getStatusCode().is2xxSuccessful()) {
            return response.getBody();
        } else {
            throw new RuntimeException("Failed to call QianWen API");
        }
    }
}